/* 对Date的扩展，将 Date 转化为指定格式的String
//月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
//年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
//例子：
//(new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
//(new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18*/
Date.prototype.Format = Date.prototype.Format || function(fmt) { // author: meizz
  fmt = fmt || 'yyyy-MM-dd hh:mm:ss'
  var o = {
    'M+': this.getMonth() + 1, // 月份
    'd+': this.getDate(), // 日
    'h+': this.getHours(), // 小时
    'H+': this.getHours(), // 小时
    'm+': this.getMinutes(), // 分
    's+': this.getSeconds(), // 秒
    'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
    'S': this.getMilliseconds() // 毫秒
  }
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }
  return fmt
}

// ---------------------------------------------------
// 判断闰年
// ---------------------------------------------------
Date.prototype.isLeapYear = function() {
  return (this.getYear() % 4 == 0 && ((this.getYear() % 100 != 0) || (this.getYear() % 400 == 0)))
}

// ---------------------------------------------------
// 日期格式化
// 格式 YYYY/yyyy/YY/yy 表示年份
// MM/M 月份
// W/w 星期
// dd/DD/d/D 日期
// hh/HH/h/H 时间
// mm/m 分钟
// ss/SS/s/S 秒
// ---------------------------------------------------
Date.prototype.FormatEx = function(formatStr) {
  var str = formatStr
  var Week = ['日', '一', '二', '三', '四', '五', '六']

  str = str.replace(/yyyy|YYYY/, this.getFullYear())
  str = str.replace(/yy|YY/, (this.getYear() % 100) > 9 ? (this.getYear() % 100).toString() : '0' + (this.getYear() %
    100))

  str = str.replace(/MM/, this.getMonth() > 9 ? this.getMonth().toString() : '0' + this.getMonth())
  str = str.replace(/M/g, this.getMonth())

  str = str.replace(/w|W/g, Week[this.getDay()])

  str = str.replace(/dd|DD/, this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate())
  str = str.replace(/d|D/g, this.getDate())

  str = str.replace(/hh|HH/, this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours())
  str = str.replace(/h|H/g, this.getHours())
  str = str.replace(/mm/, this.getMinutes() > 9 ? this.getMinutes().toString() : '0' + this.getMinutes())
  str = str.replace(/m/g, this.getMinutes())

  str = str.replace(/ss|SS/, this.getSeconds() > 9 ? this.getSeconds().toString() : '0' + this.getSeconds())
  str = str.replace(/s|S/g, this.getSeconds())

  return str
}

/**
 * 格式化时间的辅助类，将一个时间转换成x小时前、y天前等
 */
var dateUtils = {
  UNITS: {
    '年': 31557600000,
    '月': 2629800000,
    '天': 86400000,
    '小时': 3600000,
    '分钟': 60000,
    '秒': 1000
  },
  humanize: function(milliseconds) {
    var humanize = ''
    for (var Key in this.UNITS) {
      var value = this.UNITS[Key]
      if (milliseconds >= value) {
        humanize = Math.floor(milliseconds / value) + unit + '前'
        continue
      }
      break
    }
    return humanize || '刚刚'
  },
  format: function(dateStr) {
    console.log(dateStr)
    var date = this.parse(dateStr)
    var diff = Date.now() - date.getTime()
    if (diff < this.UNITS['天']) {
      return this.humanize(diff)
    }

    var _format = function(number) {
      return (number < 10 ? ('0' + number) : number)
    }
    return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDate()) + '-' + _format(
      date.getHours()) + ':' + _format(date.getMinutes())
  },
  parse: function(str) { // 将"yyyy-mm-dd HH:MM:ss"格式的字符串，转化为一个Date对象
    if (!str) {
      return new Date()
    }
    var a = str.split(/[^\.0-9]/)
    var s = a[5] || 0
    var ms = (s - Math.floor(s)) * 1000
    return new Date(a[0], a[1] - 1, a[2], a[3] || 0, a[4] || 0, s, ms)
  }
}

var utils = dateUtils

utils.fmt = 'yyyy-MM-dd hh:mm:ss'
utils.fmtYMdhms = 'yyyy-MM-dd hh:mm:ss'
utils.fmtZH = 'yyyy年MM月dd日hh时mm分ss秒'
utils.fmtYMdhmsZH = 'yyyy年MM月dd日hh时mm分ss秒'
utils.fmtMdhm = 'MM-dd hh:mm'
utils.fmtMdhmZH = 'MM月dd日hh时mm分'
utils.fmtYMdhm = 'yyyy-MM-dd hh:mm'
utils.fmtYMdhmZH = 'yyyy年MM月dd日hh时mm分'
utils.fmtYMd = 'yyyy-MM-dd'
utils.fmtYMdZH = 'yyyy年MM月dd日'
utils.fmtMd = 'MM-dd'
utils.fmtMdZH = 'MM月dd日'
utils.fmtYMd = 'yyyy-MM-dd'
utils.fmtYMdZH = 'yyyy年MM月dd日'
utils.fmthms = 'hh:mm:ss'
utils.fmthmsZH = 'hh时mm分ss秒'
utils.fmthm = 'hh:mm'
utils.fmthmZH = 'hh时mm分'

// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18*/
dateUtils.Format = function(dateStr, fmt) {
  var dt = this.parse(dateStr)
  var o = {
    'M+': dt.getMonth() + 1, // 月份
    'd+': dt.getDate(), // 日
    'h+': dt.getHours(), // 小时
    'H+': dt.getHours(), // 小时
    'm+': dt.getMinutes(), // 分
    's+': dt.getSeconds(), // 秒
    'q+': Math.floor((dt.getMonth() + 3) / 3), // 季度
    'S': dt.getMilliseconds() // 毫秒
  }
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (dt.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }
  return fmt
}

dateUtils.formatDate = dateUtils.Format

// +---------------------------------------------------
// | 求两个时间的天数差 日期格式为 YYYY-MM-dd
// +---------------------------------------------------
dateUtils.daysBetween = function(DateOne, DateTwo) {
  var OneMonth = DateOne.substring(5, DateOne.lastIndexOf('-'))
  var OneDay = DateOne.substring(DateOne.length, DateOne.lastIndexOf('-') + 1)
  var OneYear = DateOne.substring(0, DateOne.indexOf('-'))

  var TwoMonth = DateTwo.substring(5, DateTwo.lastIndexOf('-'))
  var TwoDay = DateTwo.substring(DateTwo.length, DateTwo.lastIndexOf('-') + 1)
  var TwoYear = DateTwo.substring(0, DateTwo.indexOf('-'))

  var cha = ((Date.parse(OneMonth + '/' + OneDay + '/' + OneYear) - Date.parse(TwoMonth + '/' + TwoDay + '/' +
    TwoYear)) / 86400000)
  return Math.abs(cha)
}

// +---------------------------------------------------
// | 日期计算
// +---------------------------------------------------
Date.prototype.DateAdd = function(strInterval, Number) {
  var dtTmp = new Date(this)
  switch (strInterval) {
    case 's':
      return new Date(Date.parse(dtTmp) + (1000 * Number))
    case 'n':
      return new Date(Date.parse(dtTmp) + (60000 * Number))
    case 'h':
      return new Date(Date.parse(dtTmp) + (3600000 * Number))
    case 'd':
      return new Date(Date.parse(dtTmp) + (86400000 * Number))
    case 'w':
      return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number))
    case 'q':
      return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number * 3, dtTmp.getDate(), dtTmp.getHours(),
        dtTmp.getMinutes(), dtTmp.getSeconds())
    case 'm':
      return new Date(dtTmp.setMonth(dtTmp.getMonth() + Number))
      // return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(),
      //   dtTmp.getSeconds());
    case 'y':
      return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(),
        dtTmp.getSeconds())
  }
}

Date.prototype.add = Date.prototype.DateAdd

dateUtils.getDateTimeQueryStr = function(n) {
  var now = new Date()
  if (n == 0) {
    return now.Format('yyyy-MM-dd hh:59:59')
  }
  now = now.DateAdd('d', n)
  return now.Format('yyyy-MM-dd 00:00:00')
}

dateUtils.formatDate = function(date, fmt) {
  if (fmt) {
    return date.Format(fmt)
  }
  return date.Format('yyyy-MM-dd')
}
dateUtils.formatDatehour = function(date, fmt) {
  if (fmt) {
    return date.Format(fmt)
  }
  return date.Format('yyyy-MM-dd hh:00')
}
dateUtils.formatDatehourShort = function(date, fmt) {
  if (fmt) {
    return date.Format(fmt)
  }
  return date.Format('MM-dd hh:00')
}
dateUtils.formatDatetime = function(date, fmt) {
  if (fmt) {
    return date.Format(fmt)
  }
  return date.Format('yyyy-MM-dd hh:mm:ss')
}
dateUtils.formatDatetimeShort = function(date, fmt) {
  if (fmt) {
    return date.Format(fmt)
  }
  return date.Format('MM-dd hh:mm:ss')
}
dateUtils.formatTimestamp = function(date, fmt) {
  if (fmt) {
    return date.Format(fmt)
  }
  return date.Format('yyyy-MM-dd hh:mm:ss.S')
}

// //+---------------------------------------------------
// //| 比较日期差 dtEnd 格式为日期型或者 有效日期格式字符串
// //+---------------------------------------------------
// Date.prototype.DateDiff = function(strInterval, dtEnd) {
//	var dtStart = this;
//	if(typeof dtEnd == 'string') //如果是字符串转换为日期型
//	{
//		dtEnd = StringToDate(dtEnd);
//	}
//	switch(strInterval) {
//		case 's':
//			return parseInt((dtEnd - dtStart) / 1000);
//		case 'n':
//			return parseInt((dtEnd - dtStart) / 60000);
//		case 'h':
//			return parseInt((dtEnd - dtStart) / 3600000);
//		case 'd':
//			return parseInt((dtEnd - dtStart) / 86400000);
//		case 'w':
//			return parseInt((dtEnd - dtStart) / (86400000 * 7));
//		case 'm':
//			return(dtEnd.getMonth() + 1) + ((dtEnd.getFullYear() - dtStart.getFullYear()) * 12) - (dtStart.getMonth() +
//				1);
//		case 'y':
//			return dtEnd.getFullYear() - dtStart.getFullYear();
//	}
// }
//
// //+---------------------------------------------------
// //| 日期输出字符串，重载了系统的toString方法
// //+---------------------------------------------------
// Date.prototype.toString = function(showWeek) {
//	var myDate = this;
//	var str = myDate.toLocaleDateString();
//	if(showWeek) {
//		var Week = ['日', '一', '二', '三', '四', '五', '六'];
//		str += ' 星期' + Week[myDate.getDay()];
//	}
//	return str;
// }
//
// //+---------------------------------------------------
// //| 日期合法性验证
// //| 格式为：YYYY-MM-DD或YYYY/MM/DD
// //+---------------------------------------------------
// function IsValidDate(DateStr) {
//	var sDate = DateStr.replace(/(^\s+|\s+$)/g, ''); //去两边空格;
//	if(sDate == '') return true;
//	//如果格式满足YYYY-(/)MM-(/)DD或YYYY-(/)M-(/)DD或YYYY-(/)M-(/)D或YYYY-(/)MM-(/)D就替换为''
//	//数据库中，合法日期可以是:YYYY-MM/DD(2003-3/21),数据库会自动转换为YYYY-MM-DD格式
//	var s = sDate.replace(/[\d]{ 4,4 }[\-/]{ 1 }[\d]{ 1,2 }[\-/]{ 1 }[\d]{ 1,2 }/g, '');
//	if(s == '') //说明格式满足YYYY-MM-DD或YYYY-M-DD或YYYY-M-D或YYYY-MM-D
//	{
//		var t = new Date(sDate.replace(/\-/g, '/'));
//		var ar = sDate.split(/[-/:]/);
//		if(ar[0] != t.getYear() || ar[1] != t.getMonth() + 1 || ar[2] != t.getDate()) {
//			//alert('错误的日期格式！格式为：YYYY-MM-DD或YYYY/MM/DD。注意闰年。');
//			return false;
//		}
//	} else {
//		//alert('错误的日期格式！格式为：YYYY-MM-DD或YYYY/MM/DD。注意闰年。');
//		return false;
//	}
//	return true;
// }
//
// //+---------------------------------------------------
// //| 日期时间检查
// //| 格式为：YYYY-MM-DD HH:MM:SS
// //+---------------------------------------------------
// function CheckDateTime(str) {
//	var reg = /^(\d+)-(\d{ 1,2 })-(\d{ 1,2 }) (\d{ 1,2 }):(\d{ 1,2 }):(\d{ 1,2 })$/;
//	var r = str.match(reg);
//	if(r == null) return false;
//	r[2] = r[2] - 1;
//	var d = new Date(r[1], r[2], r[3], r[4], r[5], r[6]);
//	if(d.getFullYear() != r[1]) return false;
//	if(d.getMonth() != r[2]) return false;
//	if(d.getDate() != r[3]) return false;
//	if(d.getHours() != r[4]) return false;
//	if(d.getMinutes() != r[5]) return false;
//	if(d.getSeconds() != r[6]) return false;
//	return true;
// }
//
// //+---------------------------------------------------
// //| 把日期分割成数组
// //+---------------------------------------------------
// Date.prototype.toArray = function() {
//	var myDate = this;
//	var myArray = Array();
//	myArray[0] = myDate.getFullYear();
//	myArray[1] = myDate.getMonth();
//	myArray[2] = myDate.getDate();
//	myArray[3] = myDate.getHours();
//	myArray[4] = myDate.getMinutes();
//	myArray[5] = myDate.getSeconds();
//	return myArray;
// }
//
// //+---------------------------------------------------
// //| 取得日期数据信息
// //| 参数 interval 表示数据类型
// //| y 年 m月 d日 w星期 ww周 h时 n分 s秒
// //+---------------------------------------------------
// Date.prototype.DatePart = function(interval) {
//	var myDate = this;
//	var partStr = '';
//	var Week = ['日', '一', '二', '三', '四', '五', '六'];
//	switch(interval) {
//		case 'y':
//			partStr = myDate.getFullYear();
//			break;
//		case 'm':
//			partStr = myDate.getMonth() + 1;
//			break;
//		case 'd':
//			partStr = myDate.getDate();
//			break;
//		case 'w':
//			partStr = Week[myDate.getDay()];
//			break;
//		case 'ww':
//			partStr = myDate.WeekNumOfYear();
//			break;
//		case 'h':
//			partStr = myDate.getHours();
//			break;
//		case 'n':
//			partStr = myDate.getMinutes();
//			break;
//		case 's':
//			partStr = myDate.getSeconds();
//			break;
//	}
//	return partStr;
// }
//
// //+---------------------------------------------------
// //| 取得当前日期所在月的最大天数
// //+---------------------------------------------------
// Date.prototype.MaxDayOfDate = function() {
//	var myDate = this;
//	var ary = myDate.toArray();
//	var date1 = (new Date(ary[0], ary[1] + 1, 1));
//	var date2 = date1.dateAdd(1, 'm', 1);
//	var result = dateDiff(date1.Format('yyyy-MM-dd'), date2.Format('yyyy-MM-dd'));
//	return result;
// }
//
// ////+---------------------------------------------------
// ////| 取得当前日期所在周是一年中的第几周
// ////+---------------------------------------------------
// //Date.prototype.WeekNumOfYear = function() {
// //	var myDate = this;
// //	var ary = myDate.toArray();
// //	var year = ary[0];
// //	var month = ary[1] + 1;
// //	var day = ary[2];
// //	document.write('< script language=VBScript\> \n');
// //	document.write('myDate = DateValue(''+month+' - '+day+' - '+year+'') \n');
// //	document.write('result = DatePart('ww', myDate) \n');
// //	document.write(' \n');
// //	return result;
// //}
//
// //+---------------------------------------------------
// //| 字符串转成日期类型
// //| 格式 MM/dd/YYYY MM-dd-YYYY YYYY/MM/dd YYYY-MM-dd
// //+---------------------------------------------------
// function StringToDate(DateStr) {
//
//	var converted = Date.parse(DateStr);
//	var myDate = new Date(converted);
//	if(isNaN(myDate)) {
//		//var delimCahar = DateStr.indexOf('/')!=-1?'/':'-';
//		var arys = DateStr.split('-');
//		myDate = new Date(arys[0], --arys[1], arys[2]);
//	}
//	return myDate;
// }

/* 根据出生日期算出年龄*/
dateUtils.getAge = function(strBirthday) {
  var returnAge
  var strBirthdayArr = strBirthday.split('-')
  var birthYear = strBirthdayArr[0]
  var birthMonth = strBirthdayArr[1]
  var birthDay = strBirthdayArr[2]

  d = new Date()
  var nowYear = d.getFullYear()
  var nowMonth = d.getMonth() + 1
  var nowDay = d.getDate()

  if (nowYear == birthYear) {
    returnAge = 0 // 同年 则为0岁
  } else {
    var ageDiff = nowYear - birthYear // 年之差
    if (ageDiff > 0) {
      if (nowMonth == birthMonth) {
        var dayDiff = nowDay - birthDay // 日之差
        if (dayDiff < 0) {
          returnAge = ageDiff - 1
        } else {
          returnAge = ageDiff
        }
      } else {
        var monthDiff = nowMonth - birthMonth // 月之差
        if (monthDiff < 0) {
          returnAge = ageDiff - 1
        } else {
          returnAge = ageDiff
        }
      }
    } else {
      returnAge = -1 // 返回-1 表示出生日期输入错误 晚于今天
    }
  }

  return returnAge // 返回周岁年龄
}

dateUtils.date = function(dateStr) {
  /**
   * 获取本周、本季度、本月、上月的开端日期、停止日期
   */
  var now = dateUtils.parse(dateStr) //! dateStr是,为当前日期
  // var now = new Date(); //当前日期
  if (dateStr) now = new Date(dateStr)
  var nowDayOfWeek = now.getDay() // 今天本周的第几天
  var nowHour = now.getHours() // 当前日
  var nowDay = now.getDate() // 当前日
  var nowMonth = now.getMonth() // 当前月
  var nowYear = now.getYear() // 当前年
  nowYear += (nowYear < 2000) ? 1900 : 0 //

  var lastMonthDate = new Date() // 上月日期
  lastMonthDate.setDate(1)
  lastMonthDate.setMonth(lastMonthDate.getMonth() - 1)
  var lastYear = lastMonthDate.getYear()
  var lastMonth = lastMonthDate.getMonth()

  // 格局化日期：yyyy-MM-dd
  function formatDate(date) {
    var myyear = date.getFullYear()
    var mymonth = date.getMonth() + 1
    var myweekday = date.getDate()

    if (mymonth < 10) {
      mymonth = '0' + mymonth
    }
    if (myweekday < 10) {
      myweekday = '0' + myweekday
    }
    return (myyear + '-' + mymonth + '-' + myweekday)
  }

  // 获得某月的天数
  function getMonthDays(myMonth) {
    var monthStartDate = new Date(nowYear, myMonth, 1)
    var monthEndDate = new Date(nowYear, myMonth + 1, 1)
    var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24)
    return days
  }
  this.getMonthDays = getMonthDays

  // 获得最近第n小时
  this.generateLastnhour = function(n) {
    return new Date(nowYear, nowMonth, nowDay, nowHour - n)
  }
  this.getLastnhours = function(n) {
    const arr = []
    for (let i = 0; i < n; ++i) {
      const t = new Date(nowYear, nowMonth, nowDay, nowHour - i)
      arr.push(t)
    }
    return arr
  }
  this.getTodayhours = function() {
    return this.getLastnhours(nowHour + 1)
  }

  this.getLastnhour = function(n) {
    var lastDate = new Date(nowYear, nowMonth, nowDay, nowHour - n)
    return utils.formatDatehour(lastDate)
  }
  this.getLastnhourShort = function(n) {
    var lastDate = new Date(nowYear, nowMonth, nowDay, nowHour - n)
    return utils.formatDatehourShort(lastDate)
  }

  this.getToday = function() {
    return formatDate(now)
  }
  this.getLastday = function() {
    var lastDate = new Date(nowYear, nowMonth, nowDay - 1)
    return formatDate(lastDate)
  }
  // 获得最近第n天
  this.getLastnday = function(n) {
    var lastDate = new Date(nowYear, nowMonth, nowDay - n)
    return formatDate(lastDate)
  }

  this.getThisYearStartDate = function() {
    var lastDate = new Date(nowYear, 0, 1)
    return formatDate(lastDate)
  }
  this.getThisYearEndDate = function() {
    var lastDate = new Date(nowYear, 11, 31)
    return formatDate(lastDate)
  }
  this.getLastYearStartDate = function() {
    var lastDate = new Date(nowYear - 1, 0, 1)
    return formatDate(lastDate)
  }
  this.getLastYearEndDate = function() {
    var lastDate = new Date(nowYear - 1, 11, 31)
    return formatDate(lastDate)
  }

  // 获得本季度的开端月份
  function getQuarterStartMonth() {
    var quarterStartMonth = 0
    if (nowMonth < 3) {
      quarterStartMonth = 0
    }
    if (nowMonth > 2 && nowMonth < 6) {
      quarterStartMonth = 3
    }
    if (nowMonth > 5 && nowMonth < 9) {
      quarterStartMonth = 6
    }
    if (nowMonth > 8) {
      quarterStartMonth = 9
    }
    return quarterStartMonth
  }
  this.getQuarterStartMonth = getQuarterStartMonth

  // 获得本周的开端日期
  function getWeekStartDate() {
    var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek)
    return formatDate(weekStartDate)
  }

  // 获得本周的停止日期
  function getWeekEndDate() {
    var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek))
    return formatDate(weekEndDate)
  }
  this.getWeekStartDate = getWeekStartDate
  this.getWeekEndDate = getWeekEndDate

  // 获得上周的开始日期
  function getLastWeekStartDate() {
    var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 7)
    return formatDate(weekStartDate)
  }
  // 获得上周的结束日期
  function getLastWeekEndDate() {
    var weekEndDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 1)
    return formatDate(weekEndDate)
  }
  this.getLastWeekStartDate = getLastWeekStartDate
  this.getLastWeekEndDate = getLastWeekEndDate

  // 获得本月的开端日期
  function getMonthStartDate() {
    var monthStartDate = new Date(nowYear, nowMonth, 1)
    return formatDate(monthStartDate)
  }

  // 获得本月的停止日期
  function getMonthEndDate() {
    var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth))
    return formatDate(monthEndDate)
  }
  this.getMonthStartDate = getMonthStartDate
  this.getMonthEndDate = getMonthEndDate

  // 获得上月开端时候
  function getLastMonthStartDate() {
    var lastMonthStartDate = new Date(nowYear, lastMonth, 1)
    return formatDate(lastMonthStartDate)
  }

  // 获得上月停止时候
  function getLastMonthEndDate() {
    var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth))
    return formatDate(lastMonthEndDate)
  }
  this.getLastMonthStartDate = getLastMonthStartDate
  this.getLastMonthEndDate = getLastMonthEndDate

  // 获得本季度的开端日期
  function getQuarterStartDate() {
    var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1)
    return formatDate(quarterStartDate)
  }

  // 或的本季度的停止日期
  function getQuarterEndDate() {
    var quarterEndMonth = getQuarterStartMonth() + 2
    var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth))
    return formatDate(quarterStartDate)
  }

  this.getQuarterStartDate = getQuarterStartDate
  this.getQuarterEndDate = getQuarterEndDate

  this.getLastQuarterStartDate = function() {
    var quarterStartDate = new Date(nowYear, getQuarterStartMonth() - 3, 1)
    return formatDate(quarterStartDate)
  }

  // 或的本季度的停止日期
  this.getLastQuarterEndDate = function() {
    var quarterEndMonth = getQuarterStartMonth() + 2 - 3
    var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth))
    return formatDate(quarterStartDate)
  }
}

/*
 * 输入两个日期生成时间序列
 * 输入格式：2018-01-01 （表示2018年1月）
 * 如输入201702 201809
 * 输出数组 [201702,201703,201704....,201809]
 * */
function getDateList(startDateStr, endDateStr) {
  endDateStr = (endDateStr || (new Date()).Format('yyyy-MM-dd hh:00:00'))
  var dateStart = new Date(startDateStr)
  var dateEnd = new Date(endDateStr)
  var result = [] // 结果数组
  //	while()
  var startYear = startDateStr.substr(0, 4) // 开始时间的年份
  var startMonth = startDateStr.substr(5, 2) // 开始时间的月份
  var startDay = startDateStr.substr(8, 2) // 开始时间的月份
  var endYear = endDateStr.substr(0, 4) // 结束时间的年份
  var endMonth = endDateStr.substr(5, 2) // 结束时间的月份
  var endDay = endDateStr.substr(8, 2) // 结束时间的月份
  var startDate = new Date(startYear, startMonth, startDay)
  var years = endYear - startYear - 1
  var count = 0 // 计算相隔月份
  if (years >= 0) {
    count = years * 12
    count += (12 - parseInt(startMonth))
    count += parseInt(endMonth)
  } else {
    count = parseInt(endMonth) - parseInt(startMonth)
  }
  for (var i = 0; i <= count; i++) {
    var tempYear = startDate.getFullYear().toString()
    var tempMonth = startDate.getMonth().toString().length == 1 ? '0' + startDate.getMonth().toString() : startDate.getMonth()
      .toString()
    if (tempMonth == '00') {
      tempYear = (tempYear - 1).toString()
      tempMonth = '12'
    }
    result.push(tempYear + tempMonth)
    startDate.setMonth(startDate.getMonth() + 1)
  }
  return result
}
dateUtils.getDateList = getDateList

function getHourList(startDateStr, endDateStr) {
  var now = new Date()
  endDateStr = (endDateStr || now.Format('yyyy-MM-dd hh:00:00'))
  var dateStart = new Date(startDateStr)
  dateStart = new Date(dateStart.Format('yyyy-MM-dd 00:00:00'))
  var dateEnd = new Date(endDateStr)
  var result = [] // 结果数组
  do {
    result.push(dateStart)
    dateStart = dateStart.DateAdd('h', 1)
  } while (dateStart <= dateEnd)

  return result
}

dateUtils.getHourList = getHourList

dateUtils.getLastnhour = function(n) {
  const date = new dateUtils.date()
  return date.getLastnhour(n)
}

dateUtils.getLastnday = function(n) {
  const date = new dateUtils.date()
  return date.getLastnday(n)
}


// 
import moment from 'moment'

const fmts = {
  days: 'YYYY-MM-DD',
}

dateUtils.getSeries = (startDateStr, endDateStr, t='days', intv = 1) => {
  const fmt = fmts[t] 
  let dateStart = moment(startDateStr)
  let dateEnd = moment(endDateStr || undefined)
  endDateStr = dateEnd.format(fmt)
  var result = [] // 结果数组
  do {
    startDateStr = dateStart.format(fmt)
    result.push(startDateStr)
    dateStart = dateStart.add(t,intv)
    startDateStr = dateStart.format(fmt)
  } while (startDateStr <= endDateStr)

  return result
}

//

export default dateUtils
